/**
 * Meet UI, Meet You!
 * @author jayen
 * @version v1.0.1
 */
const app = getApp();
import {
  queryCouponList,
  receiveCoupon,
} from "../../../service/jb_coupon_service.js"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      { id: '1', title: '通用券', loaded: false },
      { id: '2', title: '商品券', loaded: false },
      { id: '3', title: '品类券', loaded: false },
      // { id: 'c', title: '已过期', loaded: false }
    ],
    tabHeight: 0,
    couponType: '1',
    couponList: [

    ],
    overCouponList: [],
    isShowLoading: false,//加载状态出现
    isBottomShow: false,//是否没有更多数据了
    page: 1,
    isFirstEnter: true,
  },
  onLoad() {
    this.scrollTopCache = []; // 记录标签页滚动位置
    const windowHeight = wx.getSystemInfoSync().windowHeight; // 实时获取兼容自定义导航条情况
    this.setData({
      tabHeight: windowHeight
    });

    this.queryCouponList();
    setTimeout(() => {
      this.setData({
        isFirstEnter: false
      })
    }, 1000)
  },
  //获取列表
  queryCouponList(type) {
    wx.showLoading();
    queryCouponList(this.data.couponType, this.data.page).then(res => {
      wx.hideLoading();
      if (res.code == 200 && res.data) {
        let arr = (type == 'refresh' ? [] : this.data.couponList || []);
        arr = arr.concat(res.data.list || [])
        this.setData({
          couponList: arr,
          refreshFlag: false,
          isShowLoading: false,
          isBottomShow: res.data.totalPage == this.data.page ? true : false,
        })
      }
    }).catch(err => {
      wx.hideLoading();

      wx.showToast({
        title: err || '加载失败',
        icon: "none"
      })
      if (this.data.page != 1) {
        this.setData({
          page: this.data.page - 1
        })
      }
      this.setData({
        refreshFlag: false,
      })
      console.error("JJLog:queryCouponList", err);
    })
  },
  getCouponAction(e) {
    if(!app.globalData.isLogin){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
    let couponId = e.detail.couponId;
    let index = e.detail.index;
    let changeItem = this.data.couponList[index];
    wx.showLoading();
    receiveCoupon({ couponId: couponId }).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: "领取成功",
          icon: "success"
        })
        changeItem.isUse = true;
        let item = 'couponList[' + index + ']';
        this.setData({
          [item]: changeItem,
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || '领取失败',
        icon: "none"
      })
      console.error("JJLog:receiveCoupon", err);

    })

  },
  // 下拉刷新
  handleRefresh() {
    // TODO
    this.setData({
      page: 1,
      isBottomShow: false,
      refreshFlag: true,
    })
    this.queryCouponList('refresh');
  },
  // 加载更多
  handleLoadmore() {
    console.log('加载更多');
    if (this.data.isBottomShow) {
      return;
    }
    this.setData({
      isShowLoading: true,
      page: this.data.page + 1
    })
    this.queryCouponList()
  },
  handleScroll(e) {
    // const index = Number(e.currentTarget.dataset.index);
    // const { scrollTop } = e.detail;
    // this.scrollTopCache[index] = scrollTop;
    // this.meetBackTop.setBackTop(scrollTop, `scrollTop[${index}]`);
  },
  handleChange(e) {
    const index = e.detail.index || 0;
    console.log("JJLog:index是:" + index);
    this.setData({
      couponType: index + 1
    })
    if (!this.data.isFirstEnter) {
      this.setData({
        page: 1,
        isBottomShow: false,
        couponList: [],
      })
      this.queryCouponList()
    }

    if (!this.data.tabs[index].loaded) {

      this.setData({
        [`tabs[${index}].loaded`]: true,

      });

    }
    // this.meetBackTop.setBackTop(this.scrollTopCache[index], `scrollTop[${index}]`);
  }
});
